package com.hao.epidemic_prevention.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.hao.epidemic_prevention.bean.TCaptchaVerify;
import com.hao.epidemic_prevention.utils.JSONResult;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.net.URLEncoder;

/**
 * @ClassName TCaptchaVerifyController
 * @Descroption
 * @Author hao
 * @Date 2021/6/17 11:30
 **/

@Controller
@CrossOrigin(origins = "*")
/*@RequestMapping("/admin")*/
public class TCaptchaVerifyController {
    private static final String APP_ID = "2076199524";//AKIDQecJjslNimZ6LK2TdW7TXKp3xaw1pMcb
    private static final String APP_SECRET = "0HP_jf3Ngp9QU0kj8kM0AcA**";//jAKDpBF2cwzlFTP4SYOwDDxPaVAYfU2J
    private static final String VERIFY_URI = "https://ssl.captcha.qq.com/ticket/verify?aid=%s&AppSecretKey=%s&Ticket=%s&Randstr=%s&UserIP=%s";

    private static String msg = "";
    public static int verifyTicket(String ticket, String rand, String userIp) {
        CloseableHttpClient httpclient = HttpClients.createDefault();
        HttpGet httpGet;
        CloseableHttpResponse response = null;
        try {
            httpGet = new HttpGet(String.format(VERIFY_URI,
                    APP_ID,
                    APP_SECRET,
                    URLEncoder.encode(ticket, "UTF-8"),
                    URLEncoder.encode(rand, "UTF-8"),
                    URLEncoder.encode(userIp, "UTF-8")
            ));
            response = httpclient.execute(httpGet);

            HttpEntity entity = response.getEntity();
            if (entity != null) {
                String res = EntityUtils.toString(entity);
                System.out.println(res); // 临时输出

                JSONObject result = JSON.parseObject(res);
                // 返回码
                int code = result.getInteger("response");
                // 恶意等级
                int evilLevel = result.getInteger("evil_level");
                // 错误信息
                msg = result.getString("err_msg");

                // 验证成功
                if (code == 1) return evilLevel;
            }
        } catch (java.io.IOException e) {
            // 忽略
        } finally {
            try {
                response.close();
            } catch (Exception ignore) {
            }
        }

        return -1;
    }

    @ResponseBody
    @RequestMapping(value = "/verify", method = RequestMethod.POST)
    public JSONResult verify(@RequestBody TCaptchaVerify tCaptchaVerify) {
        int result = verifyTicket(tCaptchaVerify.getTicket(), tCaptchaVerify.getRandstr(), tCaptchaVerify.getUserip());
        if(result != -1)return new JSONResult(200, "验证成功", result);
        else return new JSONResult(500, "验证失败", msg);
    }
}
